package com.smart_campus.sys.controller;

import com.smart_campus.sys.common.ActiveUser;
import com.smart_campus.sys.common.DataGridView;
import com.smart_campus.sys.common.ResultObj;
import com.smart_campus.sys.domain.Menu;
import com.smart_campus.sys.domain.Role;
import com.smart_campus.sys.domain.User;
import com.smart_campus.sys.service.MenuService;
import com.smart_campus.sys.service.RoleService;
import com.smart_campus.sys.vo.RoleVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @aytgor 陈鹏
 * @date 2020/6/10 14:23
 * 角色contoller
 */

@RestController
@RequestMapping("Role")
public class RoleController {
    @Autowired
    private RoleService roleService;

    /**
     * @return ResultObj
     * 角色添加方法
     */
    @RequestMapping("RoleAdd")
    public ResultObj RoleAdd(Role role) {
        try {
            role.setCreateTime(new Date());
            role.setAvailable(1);
            roleService.insert(role);
            return ResultObj.INSERT_ROLE_YES;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.INSERT_ROLE_NO;
        }
    }

    /**
     * @param roleVo
     * @return DataGridView
     * 角色列表
     */
    @RequestMapping("RoleList")
    public DataGridView RoleList(RoleVo roleVo) {
        return roleService.selectRole(roleVo);
    }

    /**
     * 批量删除角色
     *
     * @param ids
     * @return ResultObj
     * 11
     */
    @RequiresPermissions("Role:deletebatch")
    @RequestMapping("DeleteBatchRole")
    public ResultObj DeleteBatchRole(Integer[] ids) {
        try {
            for (Integer id : ids) {
                roleService.deleteByPrimaryKey(id);
            }
            return ResultObj.DELETE_ROLE_YES;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ROLE_NO;
        }
    }


    /**
     * 删除角色
     *
     * @param id
     * @return ResultObj
     */
    @RequiresPermissions("Role:delete")
    @RequestMapping("DeleteRole")
    public ResultObj DeleteRole(Integer id) {
        try {
            roleService.deleteByPrimaryKey(id);
            return ResultObj.DELETE_ROLE_YES;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ROLE_NO;
        }
    }


    /**
     * 修改角色
     * @param role
     * @return ResultObJ
     */
    @RequestMapping("UpdateRole")
    public ResultObj UpdateRole(Role role) {
        try {
            roleService.updateByPrimaryKeySelective(role);
            return ResultObj.UPDATE_ROLE_YES;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ROLE_NO;
        }
    }

    /**
     * @param id
     * @return Object
     * 查询某个角色的权限
     */
    @RequestMapping("queryMenuIdsByRid")
    public Integer[] queryMenuIdsByRid(Integer id) {
        return this.roleService.selectMid(id);
    }


    /**
     * 更新角色权限
     * @param rid
     * @param mids
     * @return saveRoleMenu
     */
    @RequiresPermissions("Role:allot")
    @RequestMapping("saveRoleMenu")
    public ResultObj saveRoleMenu(Integer rid, Integer[] mids) {
        try {
            this.roleService.saveRoleMenu(rid, mids);
            return ResultObj.DISPATH_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DISPATH_ERROR;
        }
    }

    /**
     * 查询用户的角色和全部角色
     * @param userId
     * @return Object
     */
    @RequestMapping("loadAllAvailableRoleNoPage")
    public Object loadAllAvailableRoleNoPage(Integer userId){
        return this.roleService.queryAllAvailableRoleNoPage(userId);
    }}
